#include "mstp_types.h"
#include <stdio.h>

#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>


int main(void)
{
    mstp_t mstp_sock;
    struct sockaddr_in addr;
    int fd;
    int n;
    char buffer[8192*1024];
    struct timeval start, end;
    u_int32_t total = 0;
        
    addr.sin_family = AF_INET;
    addr.sin_port   = htons(2000);
    
    inet_aton("23.21.207.252", &addr.sin_addr);
    
    if (mstp_socket(&mstp_sock) == -1)
	perror("mstp_socket()");

    printf ("%#x\n", buffer);

    if (mstp_connect(&mstp_sock, &addr, NULL) == -1)
	perror("mstp_connect()");

    fd = open("/root/gcc-4.8.1.tar.gz", O_RDONLY);
    printf ("FD: %d-%d\n", fd,fd); 

    gettimeofday(&start, NULL);

    while ( (n = read(fd, buffer, 4096*1024 ) ) > 0 ) {
	total += mstp_send(&mstp_sock, buffer, n);
	printf ("%d\n", n);
    }

    gettimeofday(&end, NULL);
    
    printf ("Average Speed: %.2f MBytes/s\n", ((float)(total)/(end.tv_sec-start.tv_sec)/(1024*1024)));
    close(fd);

    pause();	
    return 0;

}